package com.cskaoyan.market.admin.controller;


import com.cskaoyan.market.admin.service.MarketTopicService;
import com.cskaoyan.market.db.domain.MarketTopic;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/topic")
public class AdminTopicController{

    @Autowired
    private MarketTopicService marketTopicService;

    @GetMapping("/read")
    private void read(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        MarketTopic marketTopics = marketTopicService.topicSelectById(id);
        List<String> goodList = new ArrayList<>();
        Map<String, Object> result = new HashMap<>();
        result.put("goodsList",goodList);
        result.put("topic",marketTopics);

        Object ok=ResponseUtil.ok(result);
        ObjectMapper objectMapper=JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);

    }
    
    @PostMapping("/create")
    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ServletInputStream inputStream = req.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = inputStream.read(bytes)) != -1) {
            byteArrayOutputStream.write(bytes, 0, length);
        }
        String requsetBody = byteArrayOutputStream.toString("utf-8");
        String content = JacksonUtil.parseString(requsetBody, "content");
        String good = JacksonUtil.parseString(requsetBody, "goods");
        String picUrl = JacksonUtil.parseString(requsetBody, "picUrl");
        String readCount = JacksonUtil.parseString(requsetBody, "readCount");
        String subtitle = JacksonUtil.parseString(requsetBody, "subtitle");
        String title = JacksonUtil.parseString(requsetBody, "title");
        String price = JacksonUtil.parseString(requsetBody, "price");
        String op1 = good.replace("[", "");
        String op2 = op1.replace("]", "");
        if(op2!=""){
            String[] split = op2.split(",");
            Integer[] goods = new Integer[split.length];
            for (int i = 0; i < split.length; i++) {
                goods[i]= Integer.valueOf(split[i]);
            }
            MarketTopic marketTopic = marketTopicService.topicCreate(title, subtitle, content, price, readCount, picUrl, goods);
            Object ok=ResponseUtil.ok(marketTopic);
            ObjectMapper objectMapper=JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(ok);
            resp.getWriter().println(s);
        }

       if(op2==""){
            Integer[] goods={};
            MarketTopic marketTopic = marketTopicService.topicCreate(title, subtitle, content, price, readCount, picUrl, goods);
            Object ok=ResponseUtil.ok(marketTopic);
            ObjectMapper objectMapper=JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(ok);
            resp.getWriter().println(s);
        }

    }

    @PostMapping("/update")
    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //  需要用到的字段      String title, String subtitle, String content, String price, String readCount, String picUrl, String sortOrder, String goods, String deleted
        ServletInputStream inputStream = req.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = inputStream.read(bytes)) != -1) {
            byteArrayOutputStream.write(bytes, 0, length);
        }
        String requsetBody = byteArrayOutputStream.toString("utf-8");
        String title = JacksonUtil.parseString(requsetBody, "title");
        String subtitle = JacksonUtil.parseString(requsetBody, "subtitle");
        String content = JacksonUtil.parseString(requsetBody, "content");
        String price = JacksonUtil.parseString(requsetBody, "price");
        String readCount = JacksonUtil.parseString(requsetBody, "readCount");
        String picUrl = JacksonUtil.parseString(requsetBody, "picUrl");
        String sortOrder = JacksonUtil.parseString(requsetBody, "sortOrder");
        String goods = JacksonUtil.parseString(requsetBody, "goods");
        String deleted = JacksonUtil.parseString(requsetBody, "deleted");
        String id = JacksonUtil.parseString(requsetBody, "id");

        List<MarketTopic> marketTopics = marketTopicService.topicUpdate(id, title, subtitle, content, price, readCount, picUrl, sortOrder, goods, deleted);
        Object ok=ResponseUtil.ok(marketTopics);
        ObjectMapper objectMapper=JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);

    }

    @PostMapping("/delete")
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ServletInputStream inputStream = req.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = inputStream.read(bytes)) != -1) {
            byteArrayOutputStream.write(bytes, 0, length);
        }
        String requsetBody = byteArrayOutputStream.toString("utf-8");
        String id = JacksonUtil.parseString(requsetBody, "id");
        marketTopicService.deleteTopic(id);
        Object ok = ResponseUtil.ok();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);
    }

    @GetMapping("/list")
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String title = req.getParameter("title");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String subtitle = req.getParameter("subtitle");
        int limit = Integer.parseInt(req.getParameter("limit"));
        int page = Integer.parseInt(req.getParameter("page"));

        List<MarketTopic> marketTopics = marketTopicService.topicSelect(title, subtitle, sort, order, limit, page);

        Object ok = ResponseUtil.okList(marketTopics);
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);
    }


}
